<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="通知角色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="noticeRole">
              <j-dict-select-tag type="list" v-model="model.noticeRole" dictCode="notice_role" placeholder="请选择通知角色" @change="noticeRoleChange(model.noticeRole)"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="接收人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="noticeNameId">
              <a-select
                show-search
                placeholder="请选择接收人"
                option-filter-prop="children"
                style="width: 100%"
                :filter-option="filterOption"
                @focus="handleFocus"
                @blur="handleBlur"
                @change="handleChange"
                v-model="model.noticeNameId"
              >
                <!--                老师-->
                <a-select-option v-for="(item,key) in noticeNameList" :value=item.teacherId :key="key" v-if="model.noticeRole==0">
                  {{item.teacherName}}
                </a-select-option>
                <!--                学生-->
                <a-select-option v-for="(item,key) in noticeNameList" :value=item.stuId :key="key" v-if="model.noticeRole==1">
                  {{item.stuName}}
                </a-select-option>
                <!--                用户-->
                <a-select-option v-for="(item,key) in noticeNameList" :value=item.userId :key="key" v-if="model.noticeRole==2">
                  {{item.userUname}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="通知标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="noticeTitle">
              <j-editor v-model="model.noticeTitle" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="通知内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="releasingAbout">
              <j-editor v-model="model.releasingAbout" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'NoticesForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           releasingAbout: [
              { required: true, message: '请输入通知内容!'},
           ],
        },
        url: {
          add: "/notices/notices/add",
          edit: "/notices/notices/edit",
          queryById: "/notices/notices/queryById",
          getTeacherList:"/teacher/teacher/getTeacherList",
          getStuList:"/stu/stu/getStuList",
          getUserList:"/users/userInfo/getUserList"
        },
        noticeNameList:[]
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      /*下拉框方法*/
      handleChange(value) {
        console.log(`selected ${value}`);
      },
      handleBlur() {
        console.log('blur');
      },
      handleFocus() {
        console.log('focus');
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      noticeRoleChange(type){
        let getUrl=""
        if (type==0){
          getUrl=this.url.getTeacherList
        }
        if (type==1){
          getUrl=this.url.getStuList
        }
        if (type==2){
          getUrl=this.url.getUserList
        }
        getAction(getUrl).then((res)=>{
          if (res.success){
            this.noticeNameList=res.result
          }
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>